$(function () {
	var vm = new Vue({
		el:'#app',
		data:{
			formData:{id:$("#id").val()},
			xtree:{},
			form:{},
			saveUrl:'',
			treeUrl:'',
			ifEdit:false
		},
		methods: {
			init:function(){
				var that = this;
				layui.use(['form','tree'], function(){
					var form = layui.form;
					if(that.formData.id){
						//修改编辑数据
						that.ifEdit = true
						that.saveUrl = staticURL+"/basic/userInfoAction/edit"
						that.treeUrl = staticURL+'/basic/userResourceAction/findResource?userId='+that.formData.id
						that.loadData(form);
					}else{
						//新增数据
						that.treeUrl = staticURL+'/basic/resourceInfoAction/resourceTree'
						that.saveUrl = staticURL+"/basic/userInfoAction/add"
					}
					var xtree = new layuiXtree({
						elem: 'authTree'           //放xtree的容器（必填）
							, form: form                       //layui form对象 （必填）
							, data: that.treeUrl   //服务端地址（必填）
							, isopen: false                     //初次加载时全部展开，默认true （选填）
							, color: "#000"                    //图标颜色 （选填）
							, icon: {                          //图标样式 （选填）
								open: "&#xe7a0;"               //节点打开的图标
								, close: "&#xe622;"            //节点关闭的图标
								, end: "&#xe621;"              //末尾节点的图标
							  }
					});
					that.xtree = xtree;
					
					form.verify({
						  repassword: function(value, item){ //value：表单的值、item：表单的DOM对象
						    if($("#password").val() && $("#password").val() != value){
						      return '密码不一致！';
						    }
						  }
					}); 
					
					//radio select vue 和layui一起用 有bug,不能自动赋值，需要设置监听，二且页面中不需要v-model
					form.on('radio(status)', function(data){
						that.formData.status = data.value;
					});
				});
				
			},
			goBack:function () { 
				parent.layer.close(parent.layer.getFrameIndex(window.name));
			},
			doSave:function () {
				if(!sy.validateLayuiForm($("#layui-form"))){
					return;
				}
				vm.userResouces();
				$.ajax({url : vm.saveUrl,
					type : "post",
					async:false,
					dataType:'json',
					data:vm.formData,
					cache : false,
					success : function(json) {
						if (json && json.success) {
							$.when(parent._refresh()).always(vm.goBack());
						}else{
							layer.msg(json.msg);
						}
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						layer.msg('操作失败！');
			       }
				}); 
			},
			userResouces:function(){
				 var oCks = vm.xtree.GetChecked(); //获取末级且选中的checkbox原dom对象，返回的类型:Array
				 var array = [];
		         for (var i = 0; i < oCks.length; i++) {
		              array.push(oCks[i].value);
		         }
		         vm.formData.resourceIds = array.join(',');
			},
			loadData:function(form){
				var formData = this.formData;
				var vm  = this;
				$.ajax({url :staticURL+"/basic/userInfoAction/showDesc",
					data : {
						id : vm.formData.id
					},
					dataType : 'json',
					cache : false,
					success : function(json) {
						vm.formData = json;
						//这一句其实只是为了给radio赋值
						sy.formLoadData($(".layui-form"),json);
						form.render();
					}
				});
			}
		},
		created: function () {
			this.init();
		},
		updated:function(){
			
		}
	});
});